package com.chase.springboot.service.impl;

import com.chase.springboot.elasticsearch.EsPostRepository;
import com.chase.springboot.mapper.TopicMapper;
import com.chase.springboot.service.IEsPostService;
import com.chase.springboot.model.vo.PostVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
@Service
public class IEsPostServiceImpl implements IEsPostService {
    @Autowired
    private TopicMapper bmsPostMapper;
    @Autowired
    private EsPostRepository postRepository;

    @Override
    public int importAll() {
        List<PostVO> list = bmsPostMapper.selectListAndPage();
        System.out.println(list.size());
        Iterable<PostVO> bmsPostsIterable = postRepository.saveAll(list);
        Iterator<PostVO> iterator = bmsPostsIterable.iterator();
        int result = 0;
        while (iterator.hasNext()){
            result ++ ;
            iterator.next();
        }
        return result;
    }

    @Override
    public Page<PostVO> search(String keyword, Integer pageNum, Integer pageSize) {
        PageRequest pageable = PageRequest.of(pageNum, pageSize);
        return postRepository.findByTitleOrUsername(keyword, keyword, pageable);
    }
}
